package com.ycsj.goldmedalnewconcept.teacher.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.image.ImageUtil;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.ScreenUtils;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.common.view.dialog.AlertDialogUtil;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.ResultInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherAddStudentScoreActivity;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherScoreManagerActivity;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherShowScoreDetailsActivity;
import com.ycsj.goldmedalnewconcept.teacher.bean.ScoreDetailsInfo;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 成绩详情列表
 * Created by Administrator on 2018/6/28 0028.
 */

public class ScortDetailsGvAapter extends BaseQuickAdapter<ScoreDetailsInfo.DataBean, BaseViewHolder> {

    private Context context;


    //最外层的布局参数
    private LinearLayout.LayoutParams lpOut;
    private RelativeLayout.LayoutParams lpInner;
    //蒙版的布局参数
    private RelativeLayout.LayoutParams lpflag;

    public ScortDetailsGvAapter(Context context, int layoutResId, @Nullable List<ScoreDetailsInfo.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
        int outWidth = (ScreenUtils.getScreenWidth(context) - UiUtils.dp2px(10 + 10)) / 3;
        lpOut = new LinearLayout.LayoutParams(outWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
        lpInner = new RelativeLayout.LayoutParams(outWidth * 4 / 5, UiUtils.dp2px(120));
        lpInner.addRule(RelativeLayout.CENTER_IN_PARENT);
    }

    @Override
    protected void convert(BaseViewHolder helper, final ScoreDetailsInfo.DataBean item) {
        RelativeLayout rlOut = helper.getView(R.id.rl_out);
        rlOut.setLayoutParams(lpOut);
        RelativeLayout rlInner = helper.getView(R.id.rl_inner);
        rlInner.setLayoutParams(lpInner);

        //没有分数时需要显示的大图头像
        RoundedImageView ivBigHead = helper.getView(R.id.iv_big_head);
        ImageUtil.displayImage(context, ivBigHead, item.getICON());

        //有分数时需要显示的分数和小头像的布局
        LinearLayout llHasScore = helper.getView(R.id.ll_has_score);

        //分数
        TextView tvScore = helper.getView(R.id.tv_score);
        tvScore.setText(item.getSCORE() + "分");

        //头像
        RoundedImageView ivHead = helper.getView(R.id.iv_head);
        ImageUtil.displayImage(context, ivHead, item.getICON(), R.drawable.head_default);

        //标记状态
        ImageView ivFlag = helper.getView(R.id.iv_flag);

        //蒙版视图
        View view = helper.getView(R.id.view);

        //姓名
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getNAME());

        if (item.getSCORE() == null) {
            //没有分数
            llHasScore.setVisibility(View.INVISIBLE);
            ivBigHead.setVisibility(View.VISIBLE);
            ivFlag.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
        } else {
            //有分数
            llHasScore.setVisibility(View.VISIBLE);
            ivBigHead.setVisibility(View.INVISIBLE);

            //家长看没看成绩,未查看不显示蒙版,查看显示蒙版item.getPARENT_STATUS()==0
            //家长回执,填写显示标签,不填写不显示  item.getSTATUS()
            //老师是否查看回执,查看灰色没查看红色 item.getTEACHER_STATUS()
            //控制蒙版的显示
            if (item.getREAD_STATUS() == 0) {
                //表示家长未查看成绩,不显示蒙版
                view.setVisibility(View.GONE);
            } else {
                //家长已查看成绩,显示蒙版
                view.setVisibility(View.VISIBLE);
            }

            //控制标签的显示
            if (item.getWRITE_STATUS() == 0) {
                //未填写回执不显示标签
                ivFlag.setVisibility(View.GONE);
            } else {
                //已填写回执,显示标签
                ivFlag.setVisibility(View.VISIBLE);
                //控制标签显示的颜色状态
//                if (item.getWRITE_STATUS() == 0) {
//                    //老师未查看回执显示
//                    ivFlag.setImageResource(R.drawable.teacher_state_one);
//                } else {
//                    ivFlag.setImageResource(R.drawable.teacher_state_two);
//                }
            }
            //长按点击事件
            rlInner.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    AlertDialogUtil.newInstance(context)
                            .setCancelable(true) //点击返回键和空白处是否可以消失
                            .negativeText("")//设置取消按钮的文字,如果设置为空时则不显示取消按钮
                            //确定按钮的点击事件
                            .positive(new AlertDialogUtil.OnAlertPositiveListener() {
                                @Override
                                public void onPositiveClick(int i) {

                                    //删除成绩
                                    doDeletScore(item);
                                }

                            })//取消按钮的点击事件
                            .showDialog("确定删除成绩吗?");

                    return true;
                }
            });
        }


        //点击事件
        rlInner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //条目点击事件
                if ("null".equals(item.getSCORE() + "")) {
                    //没有分数跳转到上传考试成绩页面
                    Intent intent = new Intent(context, TeacherAddStudentScoreActivity.class);
                    intent.putExtra("student", (Serializable) item);
                    context.startActivity(intent);
                } else {
//                    if (!TextUtils.isEmpty(item.getPARENT_CONTENT())) {
//                        //有回执,点击发一个请求给服务器表示老师看过回执
//                        sendLookDetailsAction(item);
//                    }
                    //有分数跳转到成绩详情页面
                    Intent intent = new Intent(context, TeacherShowScoreDetailsActivity.class);
                    intent.putExtra("student", (Serializable) item);
                    context.startActivity(intent);
                }
            }
        });
    }

    /**
     * 教师端删除成绩
     */
    private void doDeletScore(ScoreDetailsInfo.DataBean item) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "21");
        map.put("acc", AccountUtils.getAccount(context));
        map.put("role", AccountUtils.getRole(context));
        map.put("rowId", item.getROW_ID() + "");
        map.put("apptoken", AccountUtils.getToken(context));
        map.put("platform", "2");

        HttpUtils.post(HttpAddress.SCOREMANAGE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                        UiUtils.Toast("数据解析异常", false);
                    }
                    Log.i("test", "教师成绩数据" + response);
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            //通知成绩管理界面进行界面刷新
                            ToastUtils.showShort("删除成功");
                            BusInfo busInfo = new BusInfo();

                            busInfo.type = TeacherScoreManagerActivity.REFRESHDATA;
                            EventBus.getDefault().post(busInfo);
                        } else {
                            //返回失败
                            Log.i("test", "服务器返回异常码是:" + info.getState());
                        }
                    }
                }
            }
        });
    }


//    /**
//     * 教师端查看回执的请求
//     */
//    private void sendLookDetailsAction(ScoreDetailsInfo.DataBean item) {
//        Map<String, String> map = new HashMap<>();
//        map.put("type", "51");
//        map.put("acc", AccountUtils.getAccount(context));
//        map.put("role", AccountUtils.getRole(context));
//        map.put("studentId", item.getROW_ID() + "");
//        map.put("apptoken", AccountUtils.getToken(context));
//        map.put("platform", "2");
//
//        HttpUtils.post("", map, this, new HttpUtils.CallBack() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                UiUtils.Toast(BaseApplication.ERROR, false);
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                if (!TextUtils.isEmpty(response)) {
//                    ResultInfo info = null;
//                    try {
//                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
//                    } catch (Exception e) {
//                        Log.i("test", "数据解析异常");
//                        UiUtils.Toast("数据解析异常", false);
//                    }
//                    Log.i("test", "教师成绩数据"+response);
//                    if (null != info) {
//                        if ("200".equals(info.getState())) {
//                            //返回成功
//                            //通知成绩管理界面进行界面刷新
//                            BusInfo busInfo = new BusInfo();
//
//                            busInfo.type = TeacherScoreManagerActivity.REFRESHDATA;
//                            EventBus.getDefault().post(busInfo);
//                        } else {
//                            //返回失败
//                            Log.i("test", "服务器返回异常码是:" + info.getState());
//                        }
//                    }
//                }
//            }
//        });
//    }
}











